package com.bh.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import com.bh.biz.SaveFPBiz;

/**
 * Servlet implementation class SaveXML
 */
public class SaveXML extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log=Logger.getLogger(SaveXML.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveXML() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	        doPost(request, response);
	        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	        request.setCharacterEncoding("GBK");
	        Document doc=null;
	        String str="";
	        SAXReader reader=new SAXReader();
	        if (request.getContentLength()!=0) {
                        try {
                                doc=reader.read(request.getInputStream());
//                                doc=DocumentHelper.parseText("<?xml version=\"1.0\" encoding=\"GBK\"?><root>"
//                                                + "<fpdm>123456789012</fpdm>"
//                                                + "<fphm>12345678</fphm>"
//                                                + "<jqbh>12345678</jqbh>"
//                                                + "<skdw>国家电网保定供电公司</skdw>"
//                                                + "<sh>1234567890123</sh>"
//                                                + "<kprq>2014-01-01</kprq>"
//                                                + "<sky>张三</sky>"
//                                                + "<fkdw>中国联合网络通讯有限责任公司河北省网络联合通信有限责任分公司</fkdw>"
//                                                + "<khbh>12345678</khbh>"
//                                                + "<hjdl>100000</hjdl>"
//                                                + "<hjdf>100000.01</hjdf>"
//                                                + "<xxhj>100000.10</xxhj>"
//                                                + "<dxhj>叁佰壹拾贰万伍仟陆佰肆拾玖元整</dxhj>"
//                                                + "<xmmx>"
//                                                        + "<data name=\"所属辖区\" value=\"保定市东风路营业厅\"/>"
//                                                        + "<data name=\"一档电量\" value=\"2000\"/>"
//                                                        + "<data name=\"二档电量\" value=\"3000\"/>"
//                                                        + "<data name=\"本次暂存\" value=\"100.00\"/>"
//                                                + "</xmmx>"
//                                                + "</root>");
                                SaveFPBiz biz=new SaveFPBiz();
                                if(biz.insert(doc)==1){
                                        str="<?xml version=\"1.0\" encoding=\"GBK\"?><root><result>1</result><resultmsg>成功</resultmsg></root>";
                                }else {
                                        str="<?xml version=\"1.0\" encoding=\"GBK\"?><root><result>2</result><resultmsg>该张发票与已有发票重复，请仔细核对印刷发票号码与机打号码是否一致！</resultmsg></root>";
                                }
                                //log.debug(doc.asXML());
                                
                        } catch (DocumentException e) {
                                // TODO Auto-generated catch block
                                log.error(e);
                                str="<?xml version=\"1.0\" encoding=\"GBK\"?><root><result>0</result><resultmsg>解析XML失败</resultmsg></root>";
                        }
                        
                }else {
                        str="<?xml version=\"1.0\" encoding=\"GBK\"?><root><result>0</result><resultmsg>未接收到任何数据</resultmsg></root>";
                }
	        response.setCharacterEncoding("GBK");
                response.setContentType("text/xml;charset=GBK");
                PrintWriter out=response.getWriter();
                out.print(str);
                out.close();
	}

}
